package com.study.api;

import com.study.dto.Student;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhuzi
 * 2024/11/29 20:22
 */
@Api(tags = "测试异步")
@Slf4j
@RestController
@RequestMapping("api/sync")
public class AsyncController {
    AtomicInteger atomicInteger = new AtomicInteger(1);

    @ApiOperation("测试POST")
    @PostMapping("postjson")
    public String formPost(@RequestBody Student stu) {
        log.info("接收到数据{}", stu);
        if (atomicInteger.getAndIncrement() <= 2) {
            System.out.println(1 / 0);
        }
        return LocalDateTime.now().toString();
    }

    @Async
    //@Scheduled(fixedDelay = 1000)
    @GetMapping("/synca")
    public Mono<String> sycna() {
        log.info("{}", Thread.currentThread().getName());
        return Mono.just("Hello, World!");
    }

    /**
     * @return {@link Mono }<{@link String }>
     */
    @Async
    //@Scheduled(fixedDelay = 1000)
    @GetMapping("/syncb")
    public Mono<String> sycnb() {
        log.info("{}", Thread.currentThread().getName());
        return Mono.just("Hello, World!");
    }
}
